//
//  EmptyDataViewController.m
//  EmptyViewController
//
//  Created by iOS开发 on 2018/7/18.
//  Copyright © 2018年 iOS开发. All rights reserved.
//

#import "EmptyDataViewController.h"
#import <UIScrollView+EmptyDataSet.h>

@interface EmptyDataViewController ()<DZNEmptyDataSetDelegate, DZNEmptyDataSetSource>

@end

@implementation EmptyDataViewController

- (void)dealloc {
    NSLog(@"释放%@", [self class]);
}

// cell注册 视图添加 协议实现需要在各个页实现
- (UITableView *)tableView {
    if (!_tableView) {
        _tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, WIDTH, HEIGHT) style:UITableViewStylePlain];
        _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
        _tableView.showsVerticalScrollIndicator = NO;
        _tableView.showsHorizontalScrollIndicator = NO;
        _tableView.backgroundColor = [UIColor whiteColor];
        _tableView.emptyDataSetDelegate = self;
        _tableView.emptyDataSetSource = self;
        
    }
    return _tableView;
}

// 必须先在各页面实现flowLayout
- (UICollectionView *)collection {
    if (!_collection) {
        _collection = [[UICollectionView alloc] initWithFrame:CGRectMake(0, 0, WIDTH, HEIGHT) collectionViewLayout:_flowLayout];
        _collection.backgroundColor = [UIColor whiteColor];
        _collection.showsVerticalScrollIndicator = NO;
        _collection.showsHorizontalScrollIndicator = NO;
        _collection.emptyDataSetDelegate = self;
        _collection.emptyDataSetSource = self;
    }
    return _collection;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.view.backgroundColor = [UIColor whiteColor];
    // 默认隐藏占位图
    _isHiddenEmpty = YES;
    
}


#pragma mark - emptyDelegate
// 空白页图片
- (UIImage *)imageForEmptyDataSet:(UIScrollView *)scrollView {
    switch (_emptyStyle) {
        case kNoNetwork:
            return [UIImage imageNamed:@"empty_no_network"];
            break;
        case kNoData:
            return [UIImage imageNamed:@"empty_server_no_responding"];
            break;
        case kCustom:
            return _emptyImage;
            break;
        default:
            return nil;
            break;
    }
}
// 空白页标题
- (NSAttributedString *)titleForEmptyDataSet:(UIScrollView *)scrollView {
    NSString *title = @"";
    NSDictionary *attributes = [NSMutableDictionary dictionary];
    switch (_emptyStyle) {
        case kNoNetwork:
            title = @"网络加载失败";
            [attributes setValuesForKeysWithDictionary:@{
                                                         NSFontAttributeName:[UIFont boldSystemFontOfSize:18.0f],
                                                         NSForegroundColorAttributeName:[UIColor redColor]
                                                         }];
            break;
        case kNoData:
            title = @"暂无数据";
            [attributes setValuesForKeysWithDictionary:@{
                                                         NSFontAttributeName:[UIFont boldSystemFontOfSize:18.0f],
                                                         NSForegroundColorAttributeName:[UIColor darkGrayColor]
                                                         }];
            break;
        default:
        case kCustom:
            return _emptyTitle;
            break;
            return nil;
            break;
    }
    
    return [[NSAttributedString alloc] initWithString:title attributes:attributes];
}
// 空白页标题下 文字详情
- (NSAttributedString *)descriptionForEmptyDataSet:(UIScrollView *)scrollView {
    switch (_emptyStyle) {
        case kCustom:
            return _emptyExplain;
            break;
        default:
            return nil;
            break;
    }
}
// 设置按钮标题
- (NSAttributedString *)buttonTitleForEmptyDataSet:(UIScrollView *)scrollView forState:(UIControlState)state {
    NSString *buttonTitle = @"";
    NSMutableDictionary *attributes = [NSMutableDictionary dictionary];
    switch (_emptyStyle) {
        case kNoNetwork:
            buttonTitle = @"重试";
            [attributes setValuesForKeysWithDictionary:@{
                                                         NSFontAttributeName:[UIFont boldSystemFontOfSize:17.0f],
                                                         NSForegroundColorAttributeName:[UIColor blueColor]
                                                         }];
            break;
        case kNoData:
            buttonTitle = @"重新加载";
            [attributes setValuesForKeysWithDictionary:@{
                                                         NSFontAttributeName:[UIFont boldSystemFontOfSize:17.0f],
                                                         NSForegroundColorAttributeName:[UIColor blueColor]
                                                         }];
            break;
        case kCustom:
            return _emptyButtonTitle;
        default:
            break;
    }
    return [[NSAttributedString alloc] initWithString:buttonTitle attributes:attributes];
}
// 向上偏移量
- (CGFloat)verticalOffsetForEmptyDataSet:(UIScrollView *)scrollView {
    return -100;
}
// 控件间隙
- (CGFloat)spaceHeightForEmptyDataSet:(UIScrollView *)scrollView {
    return 10.f;
}
#warning 谨慎设置
// 按钮背景图片 可显示title
//- (nullable UIImage *)buttonBackgroundImageForEmptyDataSet:(UIScrollView *)scrollView forState:(UIControlState)state {
//    return [UIImage imageNamed:@""];
//}
// 图片颜色 (不建议设置)
//- (UIColor *)imageTintColorForEmptyDataSet:(UIScrollView *)scrollView {
//    return [UIColor yellowColor];
//}
// 按钮图片 覆盖 不显示title
//- (UIImage *)buttonImageForEmptyDataSet:(UIScrollView *)scrollView forState:(UIControlState)state {
//    return [UIImage imageNamed:@""];
//}
// 背景颜色
//- (UIColor *)backgroundColorForEmptyDataSet:(UIScrollView *)scrollView {
//    return [UIColor yellowColor];
//}



// 是否显示占位图 加载时可关闭 然后刷新试图
- (BOOL)emptyDataSetShouldDisplay:(UIScrollView *)scrollView {
    return !_isHiddenEmpty;
}
// 是否可点击
- (BOOL)emptyDataSetShouldAllowTouch:(UIScrollView *)scrollView {
    return YES;
}
#warning 控制点击事件 防止多次点击 最好添加加载视图 也可在当前视图中植入加载视图
// 视图点击事件
- (void)emptyDataSet:(UIScrollView *)scrollView didTapView:(UIView *)view {
    
}
// 按钮点击事件
- (void)emptyDataSet:(UIScrollView *)scrollView didTapButton:(UIButton *)button {
    
}

- (void)noNetworkReload {
    self.isHiddenEmpty = NO;
    self.emptyStyle = kNoNetwork;
    [_tableView reloadData];
    [_collection reloadData];
}

- (void)noDataReload {
    self.isHiddenEmpty = NO;
    self.emptyStyle = kNoData;
    [_tableView reloadData];
    [_collection reloadData];
}

#pragma mark - animation
/*
// 占位图动画
- (CAAnimation *)imageAnimationForEmptyDataSet:(UIScrollView *)scrollView {
    CABasicAnimation *animation = [CABasicAnimation animationWithKeyPath: @"transform"];
    return animation;
}
// 允许动画
- (BOOL)emptyDataSetShouldAnimateImageView:(UIScrollView *)scrollView {
    return YES;
}
*/



// 自定义View
//- (UIView *)customViewForEmptyDataSet:(UIScrollView *)scrollView {
//    UIActivityIndicatorView *activityView = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleGray];
//    [activityView startAnimating];
//    return activityView;
//}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
